package test03;

public class t1 {
    public static void main(String[] args) {
        int[] arr1 = null;
        String st1 = toString(arr1);
        System.out.println(st1);

        int[] arr2 = new int[0];
        String st2 = toString(arr2);
        System.out.println(st2);

        int[] arr3 = {1,2,3,4,5,6,7,8,9};
        String st3 = toString(arr3);
        System.out.println(st3);
    }

    public static String toString(int[] arr){
        //判断数组是否为空
        if(arr == null){
            return "空数组";
        }

        int len = arr.length-1;
        if(len == -1){
            return "[]";
        }

        StringBuilder sb1 = new StringBuilder();
        sb1.append('[');
        for (int i=0; i<arr.length; i++){
            sb1.append(arr[i]);
            if(i == len){
                return sb1.append(']').toString();
            }
            sb1.append(", ");
        }
        return String.valueOf(sb1);
    }
}
